#include "Registry.h"


CRegistry::CRegistry(void)
:m_hKey(NULL)
{
}

CRegistry::~CRegistry(void)
{
    CloseKey();
}

bool CRegistry::OpenKey( HKEY hKey, LPCTSTR szkey )
{
    return OpenKey(hKey, szkey, KEY_ALL_ACCESS);
}

bool CRegistry::OpenKey( HKEY hKey, LPCTSTR szkey, REGSAM samDesired )
{
    CloseKey();
    if (RegOpenKeyEx(hKey, szkey, 0, samDesired, &m_hKey) == ERROR_SUCCESS)
    {
        return true;
    }
    else
    {
        m_hKey = NULL;
        return false;
    }
}

bool CRegistry::CreateKey( HKEY hKey, LPCTSTR szkey )
{
    CloseKey();
    if (RegCreateKeyEx(hKey, szkey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hKey, NULL) == ERROR_SUCCESS)
    {
        return true;
    }
    else
    {
        m_hKey = NULL;
        return false;
    }
}

bool CRegistry::GetValue( LPCTSTR lpValueName, tchar* szBuffer, int nBufSize )
{
     ASSERT(m_hKey != NULL);
     ASSERT(szBuffer);
     ASSERT(nBufSize > 0);

    if (!szBuffer || nBufSize <= 0)
    {
        return false;
    }

    TCHAR szValue[4 * MKY_DEFAULT_BUF_SIZE];
    DWORD dwType = REG_SZ;
    DWORD dwSize = 0;
    bool bRet = false;
    dwSize = sizeof(szValue) / sizeof(TCHAR) - 1;

    if (RegQueryValueEx(m_hKey, lpValueName, NULL, &dwType, (LPBYTE)szValue, &dwSize) == ERROR_SUCCESS)
    {
        bRet = true;
        _tcsncpy(szBuffer, szValue, nBufSize);
    }
    else
    {
        *szBuffer = 0;
    }

    return bRet;
}

bool CRegistry::GetValue( LPCTSTR lpValueName, DWORD& dwValue )
{
    ASSERT(m_hKey != NULL);

    dwValue = 0;
    DWORD dwType = REG_DWORD;
    DWORD lpcbData = sizeof(DWORD);

    if (RegQueryValueEx(m_hKey, lpValueName, NULL, &dwType, (BYTE*)(DWORD)&dwValue, &lpcbData) == ERROR_SUCCESS)
    {
        return true;
    }

    return false;
}

bool CRegistry::SetValue( LPCTSTR lpValueName, LPCTSTR lpData )
{
    ASSERT(m_hKey != NULL);

    DWORD dwType = REG_SZ;
    if (RegSetValueEx(m_hKey, lpValueName, 0, dwType, (LPBYTE)(LPCTSTR)lpData, _tcslen(lpData)) == ERROR_SUCCESS)
    {
        RegFlushKey(m_hKey);
        return true;
    }

    return false;
}

bool CRegistry::SetValue( LPCTSTR lpValueName, DWORD dwValue )
{
    ASSERT(m_hKey != NULL);

    DWORD dwType = REG_DWORD;
    if (RegSetValueEx(m_hKey, lpValueName, 0, dwType, (LPBYTE)&dwValue, sizeof(DWORD)) == ERROR_SUCCESS)
    {
        RegFlushKey(m_hKey);
        return true;
    }

    return false;
}

bool CRegistry::DeleteKey( HKEY hKey, LPCTSTR szKey )
{
    return RegDeleteKey(hKey, szKey) == ERROR_SUCCESS; 
}


bool CRegistry::SaveKey( LPCTSTR lpszFileName )
{
    ASSERT(m_hKey != NULL);
    return RegSaveKey(m_hKey, lpszFileName, NULL) == ERROR_SUCCESS;
}

bool CRegistry::RestoreKey( LPCTSTR lpszFileName, DWORD dwFlag )
{
    ASSERT(m_hKey != NULL);
    return RegRestoreKey(m_hKey, lpszFileName, dwFlag) == ERROR_SUCCESS;
}


bool CRegistry::LoadKey( HKEY hKey, LPCTSTR lpszSubKey, LPCTSTR lpszFileName )
{
    return RegLoadKey(hKey, lpszSubKey, lpszFileName) == ERROR_SUCCESS;
}

bool CRegistry::DeleteValue( LPCTSTR lpValueName )
{
    ASSERT(m_hKey != NULL);
    return RegDeleteValue(m_hKey, lpValueName) == ERROR_SUCCESS;
}

void CRegistry::CloseKey()
{
     if (NULL != m_hKey)
     {
         RegCloseKey(m_hKey);
         m_hKey = NULL;
     }
}

//////////////////////////////////////////////////////////////////////////
//
bool CRegistry::EnableReg( bool bTrue/* = true*/ )
{
	DWORD dwValue = 0;
	bTrue ? dwValue = 0 : dwValue = 1;
	tchar* szKey = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");

	if (!CreateKey(HKEY_CURRENT_USER, szKey))
	{
		return false;
	}

	if (!SetValue(_T("DisableRegistryTools"), dwValue))
	{
		return false;
	}

	return true;
}
